{% extends 'base.html' %}
{% load static %}

{% block title %}社区讨论 - 本地有约{% endblock %}

{% block content %}
<div class="container-fluid py-4">
    <div class="row g-4">
        <!-- 左侧内容区域 -->
        <div class="col-lg-8">
            <!-- 快速发布卡片 -->
            {% if user.is_authenticated %}
            <div class="card mb-4 shadow-sm">
                <div class="card-body p-3">
                    <div class="d-flex align-items-center">
                        <img src="{{ user.get_avatar_url|default:'/static/images/default-avatar.jpg' }}"
                             class="rounded-circle me-3" width="48" height="48" alt="头像">
                        <div class="flex-grow-1">
                            <a href="{% url 'location:create_post' %}" class="btn btn-light w-100 text-start text-muted p-3 rounded-3 border-0">
                                <i class="fas fa-edit me-2"></i>分享你的活动心得或提问...
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            {% endif %}

            <!-- 社区统计 - 现在放在社区成员前面 -->
            <div class="row g-4 mb-4">
                <div class="col-12">
                    <div class="card shadow-sm">
                        <div class="card-header border-0 py-3">
                            <h5 class="mb-0 text-dark">
                                <i class="fas fa-chart-bar text-success me-2"></i>社区统计
                            </h5>
                        </div>
                        <div class="card-body">
                            <div class="row g-4 text-center">
                                <div class="col-md-4">
                                    <div class="border rounded-3 p-4 bg-light h-100">
                                        <div class="display-6 fw-bold text-primary mb-2">{{ latest_posts.count }}</div>
                                        <div class="text-muted fw-medium">今日帖子</div>
                                        <small class="text-muted">今日发布的帖子数量</small>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="border rounded-3 p-4 bg-light h-100">
                                        <div class="display-6 fw-bold text-success mb-2">{{ hot_posts.count }}</div>
                                        <div class="text-muted fw-medium">热门帖子</div>
                                        <small class="text-muted">点赞评论较多的帖子</small>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="border rounded-3 p-4 bg-light h-100">
                                        <div class="display-6 fw-bold text-warning mb-2">{{ all_users.count }}</div>
                                        <div class="text-muted fw-medium">社区成员</div>
                                        <small class="text-muted">注册用户总数</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 社区成员展示区域 - 现在放在社区统计后面 -->
            <div class="card mb-4 shadow-sm">
                <div class="card-header bg-primary bg-opacity-10 border-0 py-3">
                    <div class="d-flex justify-content-between align-items-center">
                        <h5 class="mb-0 text-dark">
                            <i class="fas fa-users text-primary me-2"></i>社区成员
                        </h5>
                        <span class="badge bg-primary">{{ all_users|length }}位成员</span>
                    </div>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        {% for member in all_users %}
                        <div class="col-md-6 col-lg-4">
                            <div class="user-card text-center p-3 border rounded-3 h-100">
                                <img src="{{ member.avatar.url|default:'/static/images/default-avatar.jpg' }}"
                                     class="rounded-circle mb-3" width="80" height="80" alt="头像">
                                <h6 class="fw-bold mb-1">{{ member.username }}</h6>
                                <p class="text-muted small mb-2">
                                    {{ member.bio|default:"这个用户很懒，什么都没写..."|truncatechars:30 }}
                                </p>
                                <div class="user-stats small text-muted mb-3">
                                    <div class="row">
                                        <div class="col-4">
                                            <div class="fw-bold text-dark">{{ member.community_posts.count }}</div>
                                            <div>帖子</div>
                                        </div>
                                        <div class="col-4">
                                            <div class="fw-bold text-dark">{{ member.organized_events.count }}</div>
                                            <div>活动</div>
                                        </div>
                                        <div class="col-4">
                                            <div class="fw-bold text-dark">{{ member.followers_count|default:0 }}</div>
                                            <div>粉丝</div>
                                        </div>
                                    </div>
                                </div>
                                <a href="{% url 'location:user_profile' member.id %}" class="btn btn-outline-primary btn-sm w-100">
                                    访问主页
                                </a>
                            </div>
                        </div>
                        {% empty %}
                        <div class="col-12 text-center py-4">
                            <i class="fas fa-users fa-2x text-muted mb-3 opacity-50"></i>
                            <p class="text-muted mb-0">暂无社区成员</p>
                        </div>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <!-- 热门帖子和最新帖子并排布局 -->
            <div class="row g-4 mb-4">
                <!-- 热门帖子 - 左侧 -->
                <div class="col-lg-6">
                    <div class="card h-100 shadow-sm">
                        <div class="card-header border-0 py-3">
                            <div class="d-flex justify-content-between align-items-center">
                                <h5 class="mb-0 text-dark">
                                    <i class="fas fa-fire text-danger me-2"></i>热门帖子
                                </h5>
                                <a href="{% url 'location:community_posts' %}?sort=hot" class="btn btn-sm btn-outline-primary rounded-pill">
                                    更多
                                </a>
                            </div>
                        </div>
                        <div class="card-body p-0">
                            {% for post in hot_posts %}
                            <div class="post-item p-3 border-bottom">
                                <div class="d-flex align-items-start">
                                    <img src="{{ post.author.avatar.url|default:'/static/images/default-avatar.jpg' }}"
                                         class="rounded-circle me-3 flex-shrink-0" width="40" height="40" alt="头像">
                                    <div class="flex-grow-1">
                                        <h6 class="mb-1 fw-bold">
                                            <a href="{% url 'location:post_detail' post.id %}" class="text-decoration-none text-dark">
                                                {{ post.title|truncatechars:30 }}
                                            </a>
                                        </h6>
                                        <p class="text-muted small mb-2 lh-sm">
                                            {{ post.content|striptags|truncatechars:60 }}
                                        </p>
                                        <div class="d-flex justify-content-between align-items-center text-muted small">
                                            <div>
                                                <span class="fw-medium">{{ post.author.username }}</span>
                                            </div>
                                            <div class="d-flex gap-2">
                                                <span><i class="fas fa-heart text-danger me-1"></i>{{ post.likes_count }}</span>
                                                <span><i class="fas fa-comment me-1"></i>{{ post.comments_count }}</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {% empty %}
                            <div class="text-center py-4">
                                <i class="fas fa-comments fa-2x text-muted mb-3 opacity-50"></i>
                                <p class="text-muted mb-0 small">暂无热门帖子</p>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>

                <!-- 最新帖子 - 右侧 -->
                <div class="col-lg-6">
                    <div class="card h-100 shadow-sm">
                        <div class="card-header border-0 py-3">
                            <div class="d-flex justify-content-between align-items-center">
                                <h5 class="mb-0 text-dark">
                                    <i class="fas fa-clock text-primary me-2"></i>最新帖子
                                </h5>
                                <a href="{% url 'location:community_posts' %}" class="btn btn-sm btn-outline-primary rounded-pill">
                                    更多
                                </a>
                            </div>
                        </div>
                        <div class="card-body p-0">
                            {% for post in latest_posts %}
                            <div class="post-item p-3 border-bottom">
                                <div class="d-flex align-items-start">
                                    <img src="{{ post.author.avatar.url|default:'/static/images/default-avatar.jpg' }}"
                                         class="rounded-circle me-3 flex-shrink-0" width="40" height="40" alt="头像">
                                    <div class="flex-grow-1">
                                        <h6 class="mb-1 fw-bold">
                                            <a href="{% url 'location:post_detail' post.id %}" class="text-decoration-none text-dark">
                                                {{ post.title|truncatechars:30 }}
                                            </a>
                                        </h6>
                                        <p class="text-muted small mb-2 lh-sm">
                                            {{ post.content|striptags|truncatechars:60 }}
                                        </p>
                                        <div class="d-flex justify-content-between align-items-center text-muted small">
                                            <div>
                                                <span class="fw-medium">{{ post.author.username }}</span>
                                                <span class="mx-1">•</span>
                                                <span>{{ post.created_at|timesince }}前</span>
                                            </div>
                                            <div class="d-flex gap-2">
                                                <span><i class="fas fa-eye me-1"></i>{{ post.views_count }}</span>
                                                <span><i class="fas fa-comment me-1"></i>{{ post.comments_count }}</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {% empty %}
                            <div class="text-center py-4">
                                <i class="fas fa-file-alt fa-2x text-muted mb-3 opacity-50"></i>
                                <p class="text-muted mb-0 small">暂无帖子</p>
                                {% if user.is_authenticated %}
                                <a href="{% url 'location:create_post' %}" class="btn btn-primary btn-sm rounded-pill mt-2">
                                    发布帖子
                                </a>
                                {% endif %}
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 右侧边栏区域 -->
        <div class="col-lg-4">
            <div class="sticky-sidebar">
                <!-- 快速导航卡片 -->
                <div class="card mb-3 shadow-sm">
                    <div class="card-header border-0 py-2">
                        <h6 class="mb-0 text-dark small fw-bold">
                            <i class="fas fa-compass text-primary me-1"></i>快速导航
                        </h6>
                    </div>
                    <div class="card-body p-2">
                        <div class="row g-1">
                            <div class="col-6">
                                <a href="{% url 'location:create_post' %}" class="btn btn-outline-primary w-100 py-1 rounded-1 small">
                                    <i class="fas fa-edit me-1"></i>发帖子
                                </a>
                            </div>
                            <div class="col-6">
                                <a href="{% url 'location:user_activities' %}" class="btn btn-outline-success w-100 py-1 rounded-1 small">
                                    <i class="fas fa-rss me-1"></i>看动态
                                </a>
                            </div>
                            <div class="col-6">
                                <a href="{% url 'location:message_list' %}" class="btn btn-outline-info w-100 py-1 rounded-1 small">
                                    <i class="fas fa-envelope me-1"></i>私信
                                </a>
                            </div>
                            <div class="col-6">
                                <a href="{% url 'location:community_posts' %}" class="btn btn-outline-warning w-100 py-1 rounded-1 small">
                                    <i class="fas fa-list me-1"></i>帖子列表
                                </a>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 活跃用户卡片 - 简单版 -->
                <div class="card mb-3 shadow-sm">
                    <div class="card-header border-0 py-2">
                        <h6 class="mb-0 text-dark small fw-bold">
                            <i class="fas fa-trophy text-warning me-1"></i>发帖达人
                        </h6>
                    </div>
                    <div class="card-body p-0">
                        {% for user in active_users %}
                        <div class="d-flex align-items-center p-2 border-bottom">
                            <!-- 排名 -->
                            <div class="me-2">
                                <span class="badge bg-light text-dark">{{ forloop.counter }}</span>
                            </div>

                            <!-- 用户头像 -->
                            <img src="{{ user.avatar.url|default:'/static/images/default-avatar.jpg' }}"
                                 class="rounded-circle me-2" width="36" height="36" alt="头像">

                            <!-- 用户信息 -->
                            <div class="flex-grow-1">
                                <div class="fw-medium text-dark small">{{ user.username }}</div>
                                <div class="text-muted smaller">
                                    📖 {{ user.post_count }} 篇帖子
                                </div>
                            </div>

                            <!-- 访问按钮 -->
                            <a href="{% url 'location:user_profile' user.id %}" class="btn btn-sm btn-outline-primary rounded-pill">
                                访问
                            </a>
                        </div>
                        {% empty %}
                        <div class="text-center py-4">
                            <i class="fas fa-trophy fa-2x text-muted mb-3 opacity-50"></i>
                            <p class="text-muted mb-0 small">暂无发帖达人</p>
                        </div>
                        {% endfor %}
                    </div>
                </div>
                <!-- 热门活动卡片 -->
                <div class="card shadow-sm">
                    <div class="card-header border-0 py-2">
                        <h6 class="mb-0 text-dark small fw-bold">
                            <i class="fas fa-calendar-alt text-warning me-1"></i>热门活动
                        </h6>
                    </div>
                    <div class="card-body p-0">
                        {% for event in popular_events|slice:":3" %}
                        <div class="p-2 border-bottom">
                            <!-- 第一行：标题和统计数据 -->
                            <div class="d-flex justify-content-between align-items-start mb-1">
                                <h6 class="small fw-bold mb-0 flex-grow-1 me-2">
                                    <a href="{% url 'location:activity_detail' event.id %}" class="text-decoration-none text-dark">
                                        {{ event.title|truncatechars:20 }}
                                    </a>
                                </h6>
                                <!-- 统计数据 -->
                                <div class="d-flex gap-2 text-nowrap">
                                    <span class="text-muted smaller" title="浏览量">👀 {{ event.views_count|default:0 }}</span>
                                    <span class="text-muted smaller" title="点赞数">👍 {{ event.likes_count|default:0 }}</span>
                                    <span class="text-muted smaller" title="收藏数">✨ {{ event.favorites_count|default:0 }}</span>
                                </div>
                            </div>

                            <!-- 第二行：位置和时间 -->
                            <div class="text-muted smaller lh-sm">
                                <div class="mb-1">
                                    <i class="fas fa-map-marker-alt me-1"></i>{{ event.location|truncatechars:15 }}
                                </div>
                                <div>
                                    <i class="fas fa-clock me-1"></i>{{ event.start_time|date:"m-d H:i" }}
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                        <div class="p-2">
                            <a href="{% url 'location:activity_list' %}" class="btn btn-outline-primary w-100 rounded-pill small">
                                <i class="fas fa-arrow-right me-1"></i>查看更多活动
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
.post-item {
    transition: all 0.3s ease;
    border-radius: 6px;
}

.post-item:hover {
    background-color: #f8f9fa;
}

.user-card {
    transition: all 0.3s ease;
}

.user-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.card {
    border: none;
    border-radius: 10px;
}

.card-header {
    background: rgba(0,0,0,0.02);
    border-bottom: 1px solid rgba(0,0,0,0.08);
}

.shadow-sm {
    box-shadow: 0 2px 6px rgba(0,0,0,0.08) !important;
}

.smaller {
    font-size: 0.75rem;
}

.lh-sm {
    line-height: 1.3;
}

.btn {
    transition: all 0.3s ease;
}

.rounded-1 {
    border-radius: 6px !important;
}

.rounded-2 {
    border-radius: 8px !important;
}

.rounded-3 {
    border-radius: 12px !important;
}

/* 社区统计样式优化 */
.display-6 {
    font-size: 2.5rem;
    font-weight: 700;
}

/* 紧凑布局样式 */
.sticky-sidebar {
    position: sticky;
    top: 20px;
}

.sticky-sidebar .card {
    margin-bottom: 12px;
}

.sticky-sidebar .card:last-child {
    margin-bottom: 0;
}

.sticky-sidebar .card-body {
    padding: 8px;
}

.sticky-sidebar .btn {
    padding: 4px 8px;
    font-size: 0.8rem;
}

.sticky-sidebar .small {
    font-size: 0.8rem;
}

/* 确保两个帖子卡片高度一致 */
.col-lg-6 .card {
    height: 100%;
    display: flex;
    flex-direction: column;
}

.col-lg-6 .card-body {
    flex: 1;
    overflow-y: auto;
    max-height: 400px;
}

/* 社区统计卡片高度统一 */
.col-md-4 .bg-light {
    min-height: 140px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

/* 用户卡片样式优化 */
.user-stats .row {
    margin: 0 -5px;
}

.user-stats .col-4 {
    padding: 0 5px;
}

/* 自定义滚动条 */
.card-body::-webkit-scrollbar {
    width: 3px;
}

.card-body::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 2px;
}

.card-body::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 2px;
}

/* 响应式调整 */
@media (max-width: 991.98px) {
    .sticky-sidebar {
        position: static;
    }

    .sticky-sidebar .card {
        margin-bottom: 16px;
    }

    .display-6 {
        font-size: 2rem;
    }
}

@media (max-width: 767.98px) {
    .display-6 {
        font-size: 1.75rem;
    }

    .col-md-4 .bg-light {
        min-height: 120px;
    }
}
</style>
{% endblock %}